package com.sims.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.sims.eo.StnUser;
import com.sims.service.IUserService;
import com.sims.service.impl.UserServiceImp;

public class AccountAction extends ActionSupport {
	String accountId;
	IUserService userService = new UserServiceImp();

	StnUser user = null;

	public StnUser getUser() {
		return user;
	}

	public void setUser(StnUser user) {
		this.user = user;
	}

	public String getAccountId() {
		return accountId;
	}

	public void setAccountId(String accountId) {
		this.accountId = accountId;
	}

	// 获取所有账号
	public String listAccount() throws Exception {
		System.out.println("-----listAccount");
		List<StnUser> list = userService.getAllStnUser();
		ActionContext ctx = ActionContext.getContext();
		ctx.put("accountList", list);
		return "list";
	}

	// 删除账号
	public String deleteAccount() throws Exception {
		System.out.println("-----deleteAccount id=" + accountId);
		Long id = Long.parseLong(accountId);
		boolean bFlag = userService.deletStnUserById(id);
		if (bFlag) {
			this.addActionMessage("删除数据成功！");
		} else {
			this.addActionMessage("删除数据失败！");
		}
		return listAccount();
	}

	// 修改账号
	public String editAccount() throws Exception {
		System.out.println("-----editAccount id=" + accountId);
		Long id = Long.parseLong(accountId);
		user = userService.getStnUserById(id);
		if (user == null) {
			// 未获取到用户对象给提示
			this.addActionError("id=" + id + ",未获取到用户对象！");
		}
		return "edit";
	}

	// 编辑账号
	@InputConfig(resultName = "edit_input")
	public String updateAccount() throws Exception {
		System.out.println("-----updateAccount");
		boolean bFlag = userService.updateStnUser(user);
		if (bFlag) {
			// 修改成功
			return "go_list";
		} else {
			this.addActionError("修改数据失败！");
			return "edit";
		}
	}

	// 编辑账号Action校验方法
	public void validateUpdateAccount() throws Exception {
		System.out.println("----validateUpdateAccount");
		if (user == null) {
			// 未获取到用户对象给提示
			this.addActionError("未获取到用户对象！");
		} else {
			if (user.getUserAccount() == null
					|| user.getUserAccount().trim().length() <= 0) {
				this.addFieldError("userAccountInfo", "请输入账号 ！");
			}
			if (user.getUserPassword() == null
					|| user.getUserPassword().trim().length() <= 0) {
				this.addFieldError("userPasswordInfo", "请输入密码 ！");
			}
		}
	}

}
